﻿

function cancelStoryRowEdit(storyId) {
    if (storyId > 0) {
        var url = addURLRamdon('/Story/ReadOnly');
        $.post(url,
            {
                id: storyId
            },
            function (data) {
                if (verificarError(data)) return;
                $('#storyRow_' + storyId).replaceWith(data);
                $('#storyRow_' + storyId).ready(function () {
                    setAlternatingStoryBackgroundColors();
                });
            }
        );
    }
    else {
        var url = addURLRamdon('/Story/ListByPriority');
        $.post(url,
            {
            },
            function (data) {
                if (verificarError(data)) return;
                $('.storyTable').replaceWith(data);
            }
       );
    }
    return;
}


//-----------codigo insertado
function getUsabilityFeatures(stroyId) {
    var resultado = '';
    var i = 0;    
    for( i = 0; i < 200; i ++ ){
        var id = "#storyUsabilityFeature_" + stroyId + "_" + i;

        var valor = $(id).val();

        if (isNaN(valor)) {            
            return resultado;
        }

        if ($(id).is(':checked')) {
            resultado = resultado + '-' + valor;
        }
        
    }    
}
//-----------fin codigo insertado


function saveStoryRowEdit(storyId) {
    var originalPriority = $('#storyPriority_' + storyId).data('originalValue');
    if (originalPriority == 0) // handle the nulled/nonexisting originalValue
        originalPriority = -9;
    var priority = $('#storyPriority_' + storyId).val();
    var userDefinedId = $('#storyUserDefinedId_' + storyId).val();
    var narrative = $('#storyNarrative_' + storyId).val();
    var points = $('#storyPoints_' + storyId).val();
    var sprintId = $('#storySprint_' + storyId).val();

    var usabilityFeatures = getUsabilityFeatures(storyId);


    if (originalPriority != priority ) {
        var url = addURLRamdon('/Story/Save');
        $.post(url,
            {
                storyId: storyId, priority: priority, sprintId: sprintId,
                userDefinedId: userDefinedId, narrative: narrative,
                originalPriority: originalPriority, points: points, usabilityFeatures: usabilityFeatures
            },
            function (data) {
                if (verificarError(data)) return;
                $('.storyTable').replaceWith(data);
            }
        );
    }
    else {
        var url = addURLRamdon('/Story/Save');
        $.post(url,
            {
                storyId: storyId, priority: priority, sprintId: sprintId,
                userDefinedId: userDefinedId, narrative: narrative,
                originalPriority: originalPriority, points: points, usabilityFeatures: usabilityFeatures
            },
            function (data) {
                if (verificarError(data)) return;
                
                $('#storyRow_' + storyId).replaceWith(data);
                $('#storyRow_' + storyId).ready(function () {
                    setAlternatingStoryBackgroundColors();
                });
            }
        );
    }

    // TODO: Implement save failure GUI
}

function deleteStory(storyId) {
    jConfirm('This action removes the element. Are you sure you want to continue?', 'Confirmation Dialog', function (r) {
        if (!r) return;
    var url = addURLRamdon("Story/Delete");
    $.post(url,
        {
            id: storyId,
            storyId: storyId
        },
        function (data) {
            if (verificarError(data)) return;
            $('.storyTable').replaceWith(data);
        }
    );
    
    // TODO: Implement delete failure GUI

    });
}

function addStoryRow(sprintId) {
    
    if ( sprintId == -1 ){
        alert("the sprint is not selected");
    }
    else{
    show_story(-1);
    }
}

function setupAddStoryFormSubmit() {
    $('#addStorySaveLinkId').click(function () {
        $('#addStoryFormId').submit();
    });
}

function setAlternatingStoryBackgroundColors() {
    $(".storyTable .storyRow").removeClass("typicalAltRows");
    $(".storyTable .storyRow:odd").addClass("typicalAltRows");    
}